//存放蛀牙哦交互逻辑js
//JavaScript模块化
var seckill = {
    //封装秒杀相关的ajax的url
    url : {
        now : function () {
            return '/seckill/time/now';
        },
        exposer : function (seckillId) {
            return '/seckill/'+seckillId+'/exposer';
        },
        kill : function (seckillId, md5) {
            return '/seckill/'+seckillId+'/'+md5+'/execution';
        }
    },
    //验证手手机号
    validatePhone : function (phone) {
        if(phone && phone.length == 11 && !isNaN(phone)){
            return true;
        }else{
            return false;
        }
    },
    handlerSeckillKill : function (seckillId, node) {
        //获取秒杀地址，控制显示逻辑，执行秒杀
        //隐藏一个按钮
        node.hide().html('<button class="btn btn-primary btn-lg" id="killBtn">开始秒杀</button>');
        //post请求
        $.post(seckill.url.exposer(seckillId),{},function (data) {
            //回调函数中处理交互流程
            if(data && data['success']){
                var exposer = data['data'];
                if(exposer['exposed']){
                    //开启秒杀
                    var md5 = exposer['md5'];
                    //获取秒杀地址
                    var killUrl = seckill.url.kill(seckillId, md5);
                    console.log("killUrl:" + killUrl)
                    //对按钮进行时间绑定，只绑定一次点击时间，防止用户连续点击
                    $("#killBtn").one('click',function () {
                        //执行秒杀的操作
                        //1.禁用按钮
                        $(this).addClass('disabled');
                        //2.发送秒杀的请求
                        $.post(killUrl, {}, function (data) {
                            if(data && data['success']){
                                var result = data['data'];
                                var status = result['status'];
                                var statusInfo = result['statusInfo'];
                                //显示秒杀结果
                                node.html(statusInfo);
                            }else{

                            }
                        });
                    });
                    node.show();
                }else{
                    //未开启秒杀
                    //根据服务器的时间处理
                    var now = exposer['now'];
                    var start = exposer['start'];
                    var end = exposer['end'];
                    //重新计算及时逻辑
                    seckill.countDown(seckillId,now,start,end);
                }
            }else{
                console.warn("data:"+data);
            }
        });
    },
    countDown : function (seckillId, nowTime, startTime, endTime) {
        var $seckillBox = $("#seckill-box");
        if(nowTime >= endTime){
            $seckillBox.html('秒杀结束!');
        }else if(nowTime < startTime){
            //秒杀未开始 计时时间绑定
            // 加1s防止 用户端的计时过程中出现时间偏移
            var killTime = new Date(startTime + 1000);
            $seckillBox.countdown(killTime,function (e) {
                //控制时间的格式
                var format = e.strftime('秒杀计时: %D天 %H时 %M分 %S秒');
                $seckillBox.html(format);
            }).on('finish.countdown',function () {
                //获取秒杀地址
                seckill.handlerSeckillKill(seckillId, $seckillBox);
            });

        }else{
            seckill.handlerSeckillKill(seckillId, $seckillBox);
        }
    },

    //详情页面秒杀逻辑
    detail : {
        //详情页面初始化
        init : function (params) {
            //用户手机验证操作，及时交互
            /**
             * 交互流程
             * 1.验证手机号
             */
            //获取Cookie中的手机号
            var killPhone = $.cookie("userPhone");
            if(!seckill.validatePhone(killPhone)){
                //没有登陆
                //绑定phone
                //控制输出
                var $killPhoneModal = $("#killPhoneModal");
                $killPhoneModal.modal('show');
                $("#killPhoneBtn").click(function () {
                    var killPhone = $("#killPhoneKey").val();
                    if(seckill.validatePhone(killPhone)){
                        $.cookie("userPhone",killPhone,{expires:7, path:"/seckill"});
                        window.location.reload();
                    }else{
                        $('#killPhoneMessage').hide().html('<label class="label label-danger">手机号错误!</label>').show(300);
                        // $("#killPhoneMessage").css("display","inline-block");
                    }
                });
            }else{
                var seckillId = params["seckillId"];
                var startTime = params["startTime"];
                var endTime = params["endTime"];
                //及时交互，获取系统时间
                $.get(seckill.url.now(),{},function (data) {
                    if(data && data['success']){
                        var now = data['data'];
                        //时间判断 计时交互
                        seckill.countDown(seckillId, now, startTime, endTime);
                    }else{
                        console.warn("data:"+data);
                    }
                });
            }
        }
    }
};